//
// Created by bbnf on 10.06.2024.
//
#include <gtest/gtest.h>
#include <filesystem>

#include "storage/todo_loader.h"

namespace {
    class TxtTodoLoaderTests : public ::testing::Test {
    private:
        std::filesystem::path m_path;
    protected:
        TodoApp::Storage::TxtTodoLoader *m_loader;
    public:
        TxtTodoLoaderTests() {
            m_path = std::filesystem::path(std::format("{}/todos.txt", BINARY_DIR));
            m_loader = new TodoApp::Storage::TxtTodoLoader(m_path);
        }

        void SetUp() override {
            std::stringstream ss;
            auto t = std::chrono::system_clock::now();
            ss << "0||Todo||description||" << 0 << "||" << t.time_since_epoch().count() << "\n";
            ss << "1||Todo1||Description1||" << 0 << "||"
               << t.time_since_epoch().count() - (static_cast<int64_t >(60 * 60 * 24))
               << "\n";

            std::fstream file;
            file.open(m_path, std::ios::out);
            if (file.is_open()) {
                file << ss.str();
            }
            file.close();
        }

        void TearDown() override {
            delete m_loader;
            std::filesystem::remove(m_path);
        }
    };


    //NOLINTNEXTLINE
    TEST_F(TxtTodoLoaderTests, TestLoadTodos) {
        auto todos = m_loader->loadTodos();

        EXPECT_EQ(todos.size(), 2);

        ASSERT_TRUE(todos[1]->done());

        todos[1]->switchDone();
        ASSERT_FALSE(todos[1]->done());
    }
}  // namespace
